// JavaScript Document
$(function(){
	$(".issue").hide();
	$(".bmenu").addClass('bmenuNormal');
	$(".btool").hide();
	$("#notation").show();
	var cpage="";
	$(".bmenu").click(function(){
		$(".issue").hide();
		$("#"+$(this).html()).fadeIn(500);
		$(".btool").hide();
		cpage=$(this).html();
		// menu active style
		$(".bmenu").css("font-weight","");
		$(this).css("font-weight","bolder");
		switch (cpage){
			case 'Customer':custGetAll();$(".btool").fadeIn(500);break;
			case 'Employee':empGetAll();$(".btool").fadeIn(500);break;
			case 'Skill':skillGetAll();$(".btool").fadeIn(500);break;
			case 'Request':reqGetAll();$(".btool").fadeIn(500);break;
			case 'Contract':conGetAll();break;
		} 
	})	
	.mouseover(function(){
		$(this).removeClass('bmenuNormal').addClass('bmenuOver');
	})
	.mouseout(function(){
		$(this).removeClass('bmenuOver').addClass('bmenuNormal');
	})
})
///////////////////// General
function correctposition(divname){
	$("#"+divname).css("top",$("#"+divname).offset().top-30).css("left",$("#"+divname).offset().left+10);
}

//////////////// 记录主UI
function bgcoverResize(){
	var top=$(document).scrollTop()+($(window).height()-$('#viewbox').height())/2-30;
	if (top<=0){top=0;}
	$("#bgcover").css("width",$(document).width()).css("height",$(window).height());
	$("#viewbox").css("left",($(document).width()-$("#viewbox").width())/2).css("top",top);
}
function viewUI(){
	removeUI();
	$("body").append("<div id='bgcover'></div>")
	.append("<div id='viewbox'><div id='bgclose'>X</div></div>");
	bgcoverResize();
	correctposition("viewbox");
	$(window).resize(function(){
		bgcoverResize();
	})
	$("#bgclose").click(function(){
		removeUI();
	})
	$("#viewbox").draggable();
}
function removeUI(){
		$("#viewsub").remove();
		$("#viewbox").remove();
		$("#bgcover").remove();
}
//////////////  记录的sub UI
function viewsubResize(){
	var top=$(document).scrollTop()+($(window).height()-$('#viewbox').height())/2-30;
	var topsub=$(document).scrollTop()+($(window).height()-$('#viewsub').height())/2-30;
	if (top<=0){top=0;}
	if (topsub<=0){topsub=0;}
	$("#viewbox").css("left",($(document).width()-$("#viewbox").width()-$("#viewsub").width())/2).css("top",top);
	$("#viewsub").css("left",($(document).width()-$("#viewsub").width()+$("#viewbox").width())/2+25).css("top",topsub);
}
function viewUIsub(){
	removeUIsub();
	$("body").append("<div id='viewsub'><div id='subclose'>X</div></div>");
	viewsubResize();
	correctposition("viewsub");
	$(window).resize(function(){
		viewsubResize();
	})
	$("#subclose").click(function(){
		removeUIsub();
	})
	$("#viewsub").draggable();
}
function removeUIsub(){
	var top=$(document).scrollTop()+($(window).height()-$('#viewbox').height())/2-30;
	if (top<=0){top=0;}
	$("#viewsub").remove();
	$("#viewbox").css("left",($(document).width()-$("#viewbox").width())/2).css("top",top);
}

///////////////////////////click event to find records  这里之前是tableEdit,点击之后的事件
function findrec(cID,title,aaa,divbox,table){
	var found=-1;
	for (i=0;i<aaa.length;i++){
		if (aaa[i][0]==cID){
			found=1;
			if (found==-1){return false;}
			else{ // 生成详细列表
				showrec (title,aaa[i],divbox,table);
			}
		}
		if(found!=-1){return false;}
	}
}
//// main detail 显示记录详细UI
function showrec(title,arr,divbox,table){
	for(q=0;q<arr.length;q++){
		showCustomize(title[q],arr[q],'#viewbox');
		switch (table){ //添加自定义详细按钮
			case 'request':
				switch (title[q]){
					case 'ReqID':
						var currid= arr[q];
						$.getJSON(
							"cRequest/get_reqskills.php?table=req_skill&recid="+currid+ "&" + Date.parse(new Date()),
							function(json){
								if (json.aaData.length>=1){
									viewUIsub();
									for (i=0;i<json.aaData.length;i++){
										showsubrec(json.title,json.aaData[i],1);
										$("#viewsub").append("<br>");
									}
								}
								else{
									viewUIsub();
									$("#viewsub").append("<span style='color:red; font-weight:bolder'>Currently, this request has no skill requeirment being setted.</span>");
								}
							}
						)
					break;
					case 'CustID':
						var currid= arr[q];
						var curriddiv= '#'+title[q];
						$.getJSON(
							"cRequest/get_cust.php?table=customer&recid="+currid+ "&" + Date.parse(new Date()),
							function(json){
								$(curriddiv).append(' - <span id="custdetail" class="notice"> '+ json.aaData[2]+' '+json.aaData[3]+' </span>');
								$('#custdetail').click(function(){
									showsubrec(json.title,json.aaData,0)
								})
							}
						)
						break;
				}
				break;
			case 'employee':
				switch (title[q]){
					case 'EmpID':
						var currid= arr[q];
						$.getJSON(
							"cEmp/get_empskills.php?table=emp_skill&recid="+currid+ "&" + Date.parse(new Date()),
							function(json){
								if (json.aaData.length>=1){
									viewUIsub();
									for (i=0;i<json.aaData.length;i++){
										showsubrec(json.title,json.aaData[i],1);
										$("#viewsub").append("<br>");
									}
								}
								else{
									viewUIsub();
									$("#viewsub").append("<span style='color:red; font-weight:bolder'>Currently, this employee has no skill registered.</span>");
								}
							}
						)
					break;
				}
				break;
		}
	}
}
//// main sub detail 显示记录详细SUB UI
function showsubrec(title,arr,hasSub){
	if (hasSub==0){	viewUIsub();}
	for(q=0;q<arr.length;q++){
		showCustomize(title[q],arr[q],'#viewsub');
	}
}
function showCustomize(thetitle,thearr,thediv){
	if (thetitle.indexOf("Note",0)>=0 || thetitle.indexOf("Desc",0)>=0){
		$(thediv).append('<p>'+thetitle+': </p><div class="notebox" id="'+thetitle+'">'+thearr.replace(/\n/g,'<br/>')+'</div>');
	}else
	if (thetitle.indexOf("Price",0)>=0){
		$(thediv).append('<p>'+thetitle+': <strong><span id="'+thetitle+'">'+thearr+' AUD</span></strong></p>');
	}else
	if (thetitle.indexOf("Status",0)>=0){
		if (thearr==0){
			$(thediv).append('<p>'+thetitle+': <strong><span id="'+thetitle+'" style="color:red">Deleted</span></strong></p>');
		}else {
			$(thediv).append('<p>'+thetitle+': <strong><span id="'+thetitle+'" style="color:green;">Activated</span></strong></p>');
		}
	}
	else{
		$(thediv).append('<p>'+thetitle+': <strong><span id="'+thetitle+'">'+thearr+'</span></strong></p>');
	}
}

////// click and display 点击表记录
function tableEdit(json,aaa,title,table,divbox){
			$("tbody>tr").css("cursor","pointer");
			$("tbody>tr").click(function(){
				viewUI();
				var clickid= $(this).children("td:first").html();
				findrec(clickid,title,aaa,divbox,table);
				$("#viewbox").append('<input type="button" id="bupdate" value=" Update "/>')
				if (table=='request'){
						$("#viewbox").append('<input type="button" id="bcontract" value=" Generating contract "/>');
						$("#bcontract").click(function(){
							creatcontract(clickid,title,aaa,divbox,table);
						})
				}
				$("#viewbox").append('<input type="button" id="bdelete" value=" Delete "/>')
				for (x=0;x<aaa.length;x++){
					if (aaa[x][0]==clickid){
						if (aaa[x][8]=='Done'){
							$('#bupdate').attr('disabled','disabled');
							$('#bcontract').attr('disabled','disabled');
							$('#bdelete').attr('disabled','disabled');
						}
					}
				}
				
				
				$("#bupdate").click(function(){	
					bgcoverResize();
					removeUI();
					viewUI();
					recsCOM(title,table);
					$('#'+title[0]).parent('p').show();
					$("#badd").hide();
					for (x=0;x<aaa.length;x++){
						if (aaa[x][0]==clickid){
							for (i=0;i<title.length;i++){
								$('#'+title[i]).val(aaa[x][i]);
								if (title[i]=='CustID'){
									$('#'+title[i]).attr('disabled','disabled');
								}
								$('#sp'+title[i]).html('√').css("color","#339900");
							}
							break;
						}
					}
					removeUIsub();	
					// display update sub div
					switch (table){
						case 'employee': // if is employee subdiv update UI
							viewUIsub(); 
							$.getJSON(
								"cEmp/get_empskillslist.php?recid="+clickid+ "&" + Date.parse(new Date()),
								function(json){
									skillEdit(json,clickid,'emp','emp');
							})
							break;
						case 'request': // if is request subdiv update UI
							viewUIsub(); 
							$.getJSON(
							"cRequest/get_reqskillslist.php?recid="+clickid+ "&" + Date.parse(new Date()),
							function(json){
								skillEdit(json,clickid,'req','request');
							})
							break;
					}
				})
				$("#bdelete").click(function(){
					$("#bupdate").attr('disabled','disabled');
					$("#bdelete").hide();
					$("#viewbox").append('<input type="button" id="bdeleteyes" value=" Are you sure? "/>')
								.append('<input type="button" id="bdeletecancel" value=" Cancel "/>');
					$('#bdeletecancel').click(function(){
						$(this).remove();
						$('#bdeleteyes').remove();
						$("#bupdate").attr('disabled','');
						$("#bdelete").show();
					})
					$('#bdeleteyes').click(function(){
						$(this).attr('disabled','disabled');
						var recedit = new Object();
						recedit['table']=table;
						recedit['recid']=$("#"+title[0]).html();
						recedit['edittype']='delete';
						$.ajax({
							url:'cBasic/basic_update.php',
							data:recedit,
							type:'get',
							cache:false,
							success:function(data){
								alert (data);
								removeUI();
								switch (table){
									case 'customer':custGetAll();break;
									case 'employee':empGetAll();break;
									case 'skill':skillGetAll();break;
									case 'request':reqGetAll();break;
									case 'contract':conGetAll();break;
								} 
							}
						})
					})
				})
			})
			tablefuns();
}
function skillEdit(json,clickid,obj,objfile){ //作为sub UI 来编辑skill 
	$("#viewsub").append('<table width="100%" border="0" cellspacing="0" cellpadding="0"><tbody></tbody></table>');
	for (x=0;x<json.aaData.length;x++){
			if (json.aaData[x][3]=='1'){
				$("#viewsub table tbody").prepend("<tr><td class='skc' style='background-color:#eee;' sid='"+json.aaData[x][0]+"'>"+json.aaData[x][1]+"</td><td class='skn' style='background-color:#eee;'>"+json.aaData[x][2]+"</td><td class='sks' style='color:green'>√</td></tr>")
			}else{
				$("#viewsub table tbody").append("<tr><td class='skc' sid='"+json.aaData[x][0]+"'>"+json.aaData[x][1]+"</td><td class='skn'>"+json.aaData[x][2]+"</td><td class='sks' style='color:#FFFFE6'>X</td></tr>");
			}
	}
				if (obj=='req'){
						$.getJSON(
							"cRequest/get_skillcount.php?recid="+clickid+ "&" + Date.parse(new Date()),
							function(json){
								if (json[0]>=json[1]){
									for (i=0;i<$('.sks').length;i++){
										if ($('.sks:eq('+i+')').html()=='X'){
											$('.sks:eq('+i+')').parent('tr').hide();
										}
									}
								}
						})
				}
	$('.sks').click(function(){
		switch ($(this).html()){
			case '√':
				$(this).parent('tr').children('td:eq(2)').html('X')
				.css('color',$(this).css('background-color'));
				$(this).parent('tr').children('td:eq(0)').css('background-color','');
				$(this).parent('tr').children('td:eq(1)').css('background-color','');
				$.ajax({
					url:"c"+objfile+"/mag_"+obj+"skills.php?edittype=del&"+obj+"id="+clickid+"&skillid="+$(this).parent('tr').children('td:first').attr('sid')+ "&" + Date.parse(new Date()),
					type:'get',
					cache:false,
					success:function(data){
						if (obj=='req'){
								$.getJSON(
									"cRequest/get_skillcount.php?recid="+clickid+ "&" + Date.parse(new Date()),
									function(json){
										if (json[0]<json[1]){
											for (i=0;i<$('.sks').length;i++){
												if ($('.sks:eq('+i+')').html()=='X'){
													$('.sks:eq('+i+')').parent('tr').show();
												}
											}	
										}
								})
						}
					}
				})
				break;
			case 'X':
				$(this).parent('tr').children('td:eq(2)').html('√')
				.css('color','green')
				$(this).parent('tr').children('td:eq(0)').css('background-color','#eee');
				$(this).parent('tr').children('td:eq(1)').css('background-color','#eee');
				$.ajax({
					url:"c"+objfile+"/mag_"+obj+"skills.php?edittype=add&"+obj+"id="+clickid+"&skillid="+$(this).parent('tr').children('td:first').attr('sid')+ "&" + Date.parse(new Date()),
					type:'get',
					cache:false,
					success:function(data){
						if (obj=='req'){
								$.getJSON(
									"cRequest/get_skillcount.php?recid="+clickid+ "&" + Date.parse(new Date()),
									function(json){
										if (json[0]<json[1]){}
										else{
											for (i=0;i<$('.sks').length;i++){
												if ($('.sks:eq('+i+')').html()=='X'){
													$('.sks:eq('+i+')').parent('tr').hide();
												}
											}							
										}
								})
						}
					}
				})
				break;
		}
	})
}

//////// Add display Textbox - inputs
function recsCOM(title,table){
	for (i=0;i<title.length;i++){
		var inputs;
		if (i ==0 ){
			inputs='<input id="'+title[i]+'" type="text" class="id" disabled="true" />';
		}
		else
		if (title[i].indexOf("Email",0)>=0){
			inputs='<textarea id="'+title[i]+'" type="textarea" class="email"/><span id="sp'+title[i]+'">*</span>';
		}else
		if (title[i].indexOf("StartDate",0)>=0 || title[i].indexOf("EndDate",0)>=0){
			inputs='<input id="'+title[i]+'" type="date" class="date"  maxlength="50"/><span id="sp'+title[i]+'">* YYYY-MM-DD</span>';
		}
		else
		if (title[i].indexOf("EmpAmount",0)>=0 || title[i].indexOf("CustID",0)>=0){
			inputs='<input id="'+title[i]+'" type="number" min="0" class="number"  maxlength="10" /><span id="sp'+title[i]+'">*</span>';
		}else 
		if (title[i].indexOf("Postcode",0)>=0){
			inputs='<input id="'+title[i]+'" type="number" min="0" class="number"  maxlength="10" /><span id="sp'+title[i]+'"></span>';
		}else 
		if (title[i].indexOf("Note",0)>=0 || title[i].indexOf("Desc",0)>=0){
			inputs='</p><p><textarea id="'+title[i]+'" type="textarea" class="textarea"/><span id="sp'+title[i]+'"></span>';
		}else
		if (title[i].indexOf("Phone",0)>=0 || title[i].indexOf("Mobile",0)>=0 || title[i].indexOf("Fax",0)>=0){
			inputs='<input id="'+title[i]+'" type="tel" min="0" class="number"  maxlength="12" /><span id="sp'+title[i]+'"></span>';
		}
		else
		if (title[i].indexOf("RegDate",0)>=0){
			inputs=new Date();	
		}else
		if (title[i].indexOf("Price",0)>=0){
			inputs='<input id="'+title[i]+'" type="text" class="number" maxlength="20"/>AUD <span id="sp'+title[i]+'">*</span>';
		}
		else {inputs='<input id="'+title[i]+'" type="text" class="text" maxlength="50"/><span id="sp'+title[i]+'">*</span>';}
		
		if (title[i].indexOf('ProcessStep',0)<0){
			$("#viewbox").append('<p>'+title[i]+': '+inputs+'</p>');
		}else{
			$("#viewbox").append('<input id="'+title[i]+'" type="text" class="text"  style="visibility:hidden;" maxlength="50" value="Waitting"/>');
		}
	}
	
	$('#'+title[0]).parent('p').hide();
	
	$("#viewbox").append('<input type="button" id="badd" value=" Add "/>')
				.append('<input type="button" id="bupdate" value=" Submit Update "/>');
	
	inputVariation();
	$("#badd").click(function(){
		AddUpdate($(this),'add',title,table);
	})
	$("#bupdate").click(function(){
		AddUpdate($(this),'update',title,table);
	})
}
function AddUpdate(obj,edittype,title,table){
	var checkpoint=0;
	for (i=1;i<$("#viewbox span").length;i++){
		if ($("#viewbox span:eq("+i+")").html()!="√" && $("#viewbox span:eq("+i+")").html()!="" ){
			checkpoint=1;
			break;
		}
		if (checkpoint==1){
			break;
		}
	}
	if (checkpoint==1){alert ("There are error input"); checkpoint=0;}
	else {
		switch (edittype){
			case "add":
				$("#badd").attr('disabled','disabled');
				break;
			case "update":
				$("#bupdate").attr('disabled','disabled');
				break;
		}
		var recedit = new Object();
		var strNote='';
		recedit['table']=table;
		recedit['recid']=$("#"+title[0]).val();
		recedit['edittype']=edittype;
		for (i=1;i<title.length;i++){
			if (title[i].indexOf('RegDate',0)<0){
					recedit[title[i]] = $("#"+title[i]).val();
				
			}
		}
		$.ajax({
			url:'cBasic/basic_update.php',
			data:recedit,
			type:'get',
			cache:false,
			success:function(data){
				alert (data);
				removeUI();
				switch (table){
					case 'customer':custGetAll();break;
					case 'employee':empGetAll();break;
					case 'skill':skillGetAll();break;
					case 'request':reqGetAll();break;
					case 'contract':conGetAll();break;
				} 
			}
		})
	}	
}
function creatcontract(clickid,title,aaa,divbox,table){
	removeUI();
	viewUI();
	findrec(clickid,title,aaa,divbox,table);
	$('#viewbox').append('<hr/>');
	
	$.getJSON(
		"cContract/get_emp.php?ReqID="+clickid+ "&" + Date.parse(new Date()),
		function(json){
			if (json.empamount>json.skilled.length){alert ('Warn: \n There are not enough employees for this request employee amount. \n ---> Request need '+json.empamount+' employee\n ---> Currently there are '+json.skilled.length+' suitable employees are listed \n --click OK to continue--');}
			if (json.skilled.length==0){
				$('#viewbox').append('<span style="color:red; font-weight:bolder;">No availabe Employee can be selected.</span><br/>< Have you selected skills for the request? >');
			}else{
				$('#viewbox').append('<p><strong>Available employees</strong></p>')
							.append('<table class="mini" id="tskilled" border="0" cellspacing="0" cellpadding="0"><thead><tr><td width="10%">EmpID</td><td width="30%">EmpName</td><td width="35%">Suitable Skill</td><td width="15%">Salary</td><td width="10%">Select</td></tr></thead></table>');
				for (i=0;i<json.skilled.length;i++){
					for (y=0;y<json.skilled[i][3].length;y++){
						$('#tskilled').append('<tr class="emp'+json.skilled[i][0]+'"><td width="10%">'+json.skilled[i][0]+' </td><td class="empName" width="30%"> '+json.skilled[i][1]+' '+json.skilled[i][2]+'</td><td width="35%">'+json.skilled[i][4][y]+'</td><td width="15%" class=salary></td><td class="empselection" empid="'+json.skilled[i][0]+'" skillid="'+json.skilled[i][3][y]+'" width="10%">-</td></tr>');
					}
				}
				
				$('.empselection').click(function(){
					if ($(this).html()=='√'){
						$('#salary').parent('td').html($('#salary').val());
						$('#salary').remove();
						for (i=0;i<$(".emp"+$(this).attr('empid')).length;i++){
							$(".emp"+$(this).attr('empid')+":eq("+i+")").css('background-color','');
							$(".emp"+$(this).attr('empid')+":eq("+i+") td:eq(4)").html('-').css('color','black');
						}
					}else
					if ($(this).html()=='-'){
						var selectednum=0;
						for (i=0;i<$(".empselection").length;i++){
							if ($(".empselection:eq("+i+")").html()=='√' && $(".empselection:eq("+i+")").attr('empid')!=$(this).attr('empid')){
								selectednum++;
							}
						}
						if (selectednum<json.empamount){
							$('#salary').parent('td').html($('#salary').val());
							$('#salary').remove();
							for (i=0;i<$(".emp"+$(this).attr('empid')).length;i++){
								$(".emp"+$(this).attr('empid')+":eq("+i+")").css('background-color','#FFF0F0');
								$(".emp"+$(this).attr('empid')+":eq("+i+") td:eq(4)").html('-').css('color','black');
							}
							$(this).html('√').css('color','green');
							$(this).parent('tr').css('background-color','#DFFFE8');
							var salary=$(this).parent('tr').children('td:eq(3)').html();
							$(this).parent('tr').children('td:eq(3)').html('<input id="salary" type="text" style="width:80%;"/>');
							$('#salary').val(salary)
										.keyup(function(){
											if ($('#salary').val()!='' && $('#salary').val().toString().search(/^-?[0-9]+$/) != 0){
												alert ('The salary is not a integer!');
												$('#salary').val('');
											}
										})
						}
						else {alert ('You have selected enough amount of employees for this request.');}
					}
					$('.empselection').click(function(){$('#salary').focus();})
				})
			}
			
			if (json.empamount<=json.skilled.length){
				$('#viewbox').append('<input type="button" id="genContract" value="Generating Contract for selected employees"/>');
			}
			$('#genContract').click(function(){
				$('#salary').parent('td').html($('#salary').val());
				$('#salary').remove();
				var emps= new Array();
				var skills= new Array();
				var salarys= new Array();
				var nextindex=0;
				var everyok=0;
				for (i=0;i<$(".empselection").length;i++){
					if ($(".empselection:eq("+i+")").html()=='√'){
						emps[nextindex]=$(".empselection:eq("+i+")").attr('empid');
						skills[nextindex]=$(".empselection:eq("+i+")").attr('skillid');
						if ($(".empselection:eq("+i+")").parent('tr').children('td:eq(3)').html()==''){
							alert ('some of the selected employee do not have salary setted');
							everyok=1;
							break;
						}else
						if ($(".empselection:eq("+i+")").parent('tr').children('td:eq(3)').html().toString().search(/^-?[0-9]+$/) != 0){
							alert ("some of the selected employee's salary is not number");
							everyok=1
							break;
						}else {
							salarys[nextindex]=$(".empselection:eq("+i+")").parent('tr').children('td:eq(3)').html();
							nextindex++;
						}
					}
				}
				if (everyok==0){
						$.getJSON(
							"cRequest/get_reqskillslist.php?recid="+clickid+ "&" + Date.parse(new Date()),
							function(json){
								var checkit=new Array();
								var counter=0;
								var check;
								for (i=0;i<json.aaData.length;i++){
									if (json.aaData[i][3]==1){
										check=0;
										for (q=0;q<skills.length;q++){
											if (json.aaData[i][0]==skills[q]){
												check=1;
											}											
										}
										if (check==0){
											checkit[counter]=json.aaData[i][2];
											counter++;
										}
									}
								}
								var notice='';
								if (checkit.length>=1){
									for (i=0;i<checkit.length;i++){
										notice=notice+'\n ---> '+checkit[i];
									}
									alert ('Error\n The following skilled employees are not seleted which should be included in the request '+notice);
								}else {//插入操作 需要emps skills
									var emplist=emps.join('|');
									var skilllist=skills.join('|');
									var salarylist=salarys.join('|');
									$.ajax({
										url:'cContract/set_contract.php?empid='+emplist+'&skillid='+skilllist+'&salary='+salarylist+'&reqid='+clickid+'&'+ Date.parse(new Date()),
										type:'get',
										cache:false,
										success:function(data){
											alert ('Creat Done \n Now you can go to Contract page to view detail contracts');
											removeUI();
											reqGetAll();
										}
									})
								}
						})
				}
			})
			
			
			$('#viewbox').append('<hr/>');
			
			if (json.ignored.length==0){
				$('#viewbox').append('<span style="color:green; font-weight:bolder;">No Employee has contract which conflit with this request Start and End Date.</span><br/>');
			}else{
				$('#viewbox').append('<p><strong>Employee with Conflit Contract Date</strong></p>')
							.append('<table class="mini" id="tignored" border="0" cellspacing="0" cellpadding="0"><thead><tr><td width="15%">EmpID</td><td width="35%">EmpName</td><td width="50%">Current Working Date</td></tr></thead></table>');
				for (i=0;i<json.ignored.length;i++){
						$('#tignored').append('<tr><td width="15%">'+json.ignored[i][0]+'</td><td width="35%">'+json.ignored[i][3]+' '+json.ignored[i][4]+'</td><td width="50%">'+json.ignored[i][1]+' to '+json.ignored[i][2]+'</td></tr>');
				}
			}
	})
}

function inputVariation(){
	$('.text').blur(function(){
		if ($(this).val()!=''){
			if (($(this).val()).length==0)
				{$('#sp'+$(this).attr('id')).html('* require input');}
			else {$('#sp'+$(this).attr('id')).html('√');}
			if ($('#sp'+$(this).attr('id')).html()=="√") {$('#sp'+$(this).attr('id')).css("color","#339900");}
			else {$('#sp'+$(this).attr('id')).css("color","#c00");}
		}
	})
	$('.email').blur(function(){
		if ($(this).val()!=''){
			if ($(this).val().indexOf('@',0)<0 || $(this).val().indexOf('.',0)<0 )
			{$('#sp'+$(this).attr('id')).html($(this).val()+' is not a valid email format');}
			else {$('#sp'+$(this).attr('id')).html('√');}
			if ($('#sp'+$(this).attr('id')).html()=="√") {$('#sp'+$(this).attr('id')).css("color","#339900");}
			else {$('#sp'+$(this).attr('id')).css("color","#c00");}
		}else {$('#sp'+$(this).attr('id')).html('* require input');}
	})
	$('.date').blur(function(){
		if ($(this).val()!=''){
			if (($(this).val()).length==0)
				{$('#sp'+$(this).attr('id')).html('* require input');}
			else
			if ($(this).val().toString().search(/^\d{4}\-\d{2}\-\d{2}$/) == 0 )
			{$('#sp'+$(this).attr('id')).html('√');}
			else {$('#sp'+$(this).attr('id')).html($(this).val()+' is not a valid date format');}
			if ($('#sp'+$(this).attr('id')).html()=="√") {$('#sp'+$(this).attr('id')).css("color","#339900");}
			else {$('#sp'+$(this).attr('id')).css("color","#c00");}
			
			if ($('#StartDate').val()!='' && $('#EndDate').val()!=''){
				var rstart=($('#StartDate').val()).replace('-','');
				var rend=($('#EndDate').val()).replace('-','');
				 if (rstart >rend){
					 alert ('Date setting is incorrect - \n where start date '+$('#StartDate').val()+' is later than '+$('#EndDate').val())
				 	$('#spStartDate').html('Date Incorrect').css("color","#c00");
				 	$('#spEndDate').html('Date Incorrect').css("color","#c00");
				 }else{
				 	$('#spStartDate').html('√').css("color","#339900");
				 	$('#spEndDate').html('√').css("color","#339900");
				}
			}
		}
	})
	$('.number').blur(function(){
		if ($(this).val()!=''){
			if ($(this).val().toString().search(/^-?[0-9]+$/) != 0)
			{$('#sp'+$(this).attr('id')).html($(this).val()+' is not a number');}
			else {$('#sp'+$(this).attr('id')).html('√');}
			if ($('#sp'+$(this).attr('id')).html()=="√") {$('#sp'+$(this).attr('id')).css("color","#339900");}
			else {$('#sp'+$(this).attr('id')).css("color","#c00");}
		}
	})
}

//////////creat new rec
function recsNew(title,table){
	viewUI();
	recsCOM(title,table);
	$("#"+title[0]).hide();
	$("#sp"+title[0]).hide();
	$("#bupdate").hide();
}